package Modelo;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Observable;
import java.util.Observer;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JOptionPane;
//import Modelo_del_proyecto.*;

public class GestorTicketCine implements Observer{

         
        protected Timer timer;
	protected TicketCine ticketCine;
        protected int periodoBackup;
	public XStream xstream;
	protected GestorEventos gestorEventos;

        /**
	 * 
	 * @param pBackup
	 */
    public GestorTicketCine(int pBackup) {
        xstream = new XStream(new DomDriver());
        xstream.alias("tiketCine", TicketCine.class);
        xstream.alias("administrador", Administrador.class);
        xstream.alias("cine", Cine.class);
        xstream.alias("entrada", Entrada.class);
        xstream.alias("pelicula", Pelicula.class);
        xstream.alias("sesion", Sesion.class);
        xstream.alias("provincia", Provincia.class);
        xstream.alias("sala", Sala.class);
        xstream.alias("usuario", Usuario.class);
        xstream.alias("usuarioReg", UsuarioRegistrado.class);
        xstream.alias("usuarioGenerico", UsuarioGenerico.class);
        //xstream.omitField(TicketCine.class, "sesion");
  	//Se realiza la primera vez para tener un ejemplo de bibloteca en XML
            
            
            ticketCine = ticketCine.getInstance();
            
            
//Para el resto de veces cargar de fichero
        try {
            FileInputStream file_input_xml = new FileInputStream(new File("src/TicketCineBackup.xml"));
            ticketCine = (TicketCine) xstream.fromXML(file_input_xml);
            
        } catch (FileNotFoundException e) {
            Vista.Error no_tiketcine = new Vista.Error("ERROR no se encuentra TicketCine -> Generando TicketCine Default");
            no_tiketcine.setVisible(true);
            no_tiketcine.setAlwaysOnTop(true);
            System.out.println("ERROR no se encuentra TicketCine -> Generando TicketCine Default");
            e.printStackTrace();
            
            ticketCine = new TicketCine();
            ticketCine.cargarDatos();
            realizarBackup();
         
        }
        programarBackup(pBackup);
    }

    private void programarBackup(int pBackup) {
        int retardo_inicial = pBackup * 1000; //en milisegundos
        int periodo = pBackup * 10000; //en milisegundos
        timer = new Timer();
        TimerTask timerTask = new TimerTask()
        {
             @Override
             public void run() 
             {
                 System.out.println("Hola hago el backup");
                 realizarBackup();
                 notificarCambios();
             }
        }; 
        timer.schedule(timerTask, retardo_inicial, periodo);
        
    }

//    private void Backup() {
//        String xml = xstream.toXML(ticketCine);
//        //System.out.println(xml);
//        try {
//            FileWriter file_output_xml = new FileWriter(new File("src/TicketCine.xml"));
//            file_output_xml.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
//            file_output_xml.write(xml);
//            file_output_xml.close();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }
    
    
    public void realizarBackup() {
        //ticketCine = TicketCine.getInstance();
        
        String xml = xstream.toXML(ticketCine);
        System.out.println(xml);
        try {
            
            FileWriter file_output_xml = new FileWriter(new File("src/TicketCineBackup.xml"));
            file_output_xml.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
            file_output_xml.write(xml);
            file_output_xml.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    public void notificarCambios() {
        java.util.Date fecha = new java.util.Date();
        String info = "Guardado XML con fecha " + fecha.toString();
        System.out.println(info);
        GestorEventos.getInstance().publicar("Cambios",info);
        GestorEventos.getInstance().suscribir(this,"Cambios");
        
    }
    
    public Usuario entrar() {
        Usuario u = ticketCine.entrar();
        u.setGestorTicketCine(this);
        return u;
    }
    
    public void salir() {
        realizarBackup();
    }
    
    public TicketCine getTicketCine() {
        return ticketCine;
    }

    public void setTicketCine(TicketCine ticketCine) {
        this.ticketCine = ticketCine;
    }

    @Override
    public void update(Observable o, Object arg) {
        JOptionPane.showMessageDialog(null, (String) arg, "Aviso", JOptionPane.WARNING_MESSAGE);
    }

   
    
    
}
 